/************************************************
*
*   filename:   Engine.cpp
*   date:       11/15/09
*
*   Created by
*       Kassandra Kaplan
*
*   purpose:
*       Game engine main
*
*   Revisions:
*       - 11/28/09
*            Created initial methods
             Added logic to method Update
             Added playsound() function
             Logic Finished.
*
*   copyright 2009 by
*       Cy Cook
*       Kassandra Kaplan
*       Blaise Millegan
*       Jason Stafford
*
************************************************/
#include <iostream>
#include "enemy.h"
#include <ctime>
#include <cstdlib>
#include "SDL/SDL.h"
#include "myaudio.h"
#include "mygrafx.h"
using namespace std;


enemy::enemy()
{
    isAlive = true;
    focus = false;

    chose_heal = false;
    chose_attack = false;
    selected_player = 0;
    my_health = 300;
    attack = 30;

    players_health[0]=0;
    players_health[1]=0;
    players_health[2]=0;

    srand( (unsigned)time( NULL ) );
}

int enemy::update(int player1, int player2, int player3)

{
    int num_players=3;
    players_health[0]=player1;
    players_health[1]=player2;
    players_health[2]=player3;

    chose_heal = false;
    chose_attack = false;


    bool players_alive[num_players];


    for(int i=0;i<num_players;i++)
    {
        if (players_health[i] > 0)
        players_alive[i]=true ;
    }

    int num_playersalive=0;
    int total_health=0;
    int average_health=0;

    for (int i=0;i<num_players;i++)
    {
        if (players_alive[i])
            num_playersalive++;

        total_health+=players_health[i];
    }
    //Calculate average health;
    average_health = total_health/num_playersalive;

    //Attack if weakest character is less has less than 15 health.

    //Start of logic section
    if(my_health > 30)
    {
        //Generating random number to either attack strongest or weakest
        int high_low;
        //srand( (unsigned)time( NULL ) );
        int randomNum = rand() % 2;
        high_low = randomNum;

        //Determining which player to Attack
        int lowest_playerlife;
        int theplayer=0;
        if(high_low == 0)
        {
            lowest_playerlife = 1000;
            //find weakest player
            for(int a=0;a<num_players;a++)
            {
                if (players_health[a] < lowest_playerlife && players_health[a] > 0)
                {
                    lowest_playerlife = players_health[a];
                    theplayer=a;
                    chose_attack = true;
                }
                else
                {
                    //all players are dead
                    //break;
                }
            }
        }

        else
        {
            //find strongest player ubless there is a player below 15
            int highest_playerlife = 0;
            for(int a=0;a<num_players;a++)
            {
                if (players_health[a] >= highest_playerlife && players_health[a] > 0)
                {
                    highest_playerlife = players_health[a];
                    theplayer=a;
                    chose_attack = true;
                }
            }
            //check if any player is below 15 if not the player above is the one to attack

            int isbelow=15;
            for(int a=0;a<num_players;a++)
            {
                if(players_health[a]<isbelow && players_health[a] > 0)
                {
                    isbelow = players_health[a];
                    theplayer=a;
                    chose_attack = true;
                }
            }
        }
        selected_player = theplayer;

    }
    else
    {
        chose_heal = true;

        int try_heal;
        //srand( (unsigned)time( NULL ) );
        int randomNum = rand() % 3;
        try_heal = randomNum;

        if(try_heal == 1)
        {
            my_health = my_health + 0;
        }

        //My health is below 15 so heal no matter what.
    }
    if (chose_attack)
    {
        int randomNum = rand() % 10;
        attack = 30 + randomNum;
    }

    int myfinalchoice=0;
    if(chose_attack)
    {
        myfinalchoice = myfinalchoice + ((selected_player + 1)*100);
        myfinalchoice = myfinalchoice + attack;
    }

    return myfinalchoice;

}
// End of Update
int enemy::getHealth()
{
    return my_health;
}
void enemy::setDamage(int dam)
{
    my_health=my_health-dam;
    if(my_health < 0)
        my_health=0;
}

bool enemy::attemptHeal()
{
    int healrand = rand() % 3;

    if(healrand ==0 && my_health<280 && my_health != 0)
    {
        my_health=my_health +20;
        return true;
    }
    else
        return false;
}

bool enemy::alive()
{
    if(my_health > 0)
        isAlive = true;
    else
        isAlive=false;

    return isAlive;
}

//SDL Draw function
void enemy::draw(int x, int y, SDL_Surface *screen)
{
    //mygrafx dragonPNG("demon.png");
    //dragonPNG.apply_surface(x,y, screen);
    //SDL_Flip(dragonPNG.screen);

}

void enemy::playsound()
{
    myaudio enemy("aargh.wav");
    enemy.play();
}

void enemy::setFocus(bool isFocus)
{
     focus=isFocus;
}

bool enemy::getFocus()
{
    return focus;
}
